package cn.com.qtgl.apis.iam.comp.service;

import cn.com.qtgl.apis.iam.model.input.TripleInput;
import cn.com.qtgl.apis.iam.model.vo.Triple;
import cn.melonlib.cores.commons.comp.web.ctrl.BasicController;
import cn.melonlib.cores.graphql.comp.utils.GraphQLTools;
import cn.melonlib.objects.maps.JsonMap;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class TripleService extends ApplicationObjectSupport {

    @Resource
    private OkHttpClient httpClient;

    @Resource
    private UserIamConfig userIamConfig;

    @Resource
    private GraphQLTools graphQLTools;

    @Resource
    private ObjectMapper jsonMapper;


    public Triple addTriple(TripleInput triple) throws Exception{
        MediaType mediaType=MediaType.parse(BasicController.APPLICATION_JSON_UTF8_VALUE);
        StringBuilder mutation= new StringBuilder("mutation addTriple($triple:TripleInput!){ addTriple(triple:$triple)")
                .append(graphQLTools.createQuery(new GraphQLTools.QueryTypeReference<Triple>(){}))
                .append("}");
        JsonMap<Object> params=JsonMap.createMap().append("operationName","addTriple")
                .append("query",mutation.toString())
                .append("variables",JsonMap.createMap().append("triple",triple));
        logger.info("url:"+userIamConfig.getIam());
        logger.info(params.jsonPettyString());
        RequestBody requestBody=RequestBody.create(params.jsonString(),mediaType);
        Request request=new Request.Builder().url(userIamConfig.getIam()).post(requestBody).build();
        try(Response response=httpClient.newCall(request).execute()){
            String body=response.body().string();
            return jsonMapper.readValue(body, new TypeReference<Map<String,Map<String,Triple>>>() {}).get("data").get("addTriple");
        }
    }
    public Triple deleteTriple(String id) throws Exception{
        MediaType mediaType=MediaType.parse(BasicController.APPLICATION_JSON_UTF8_VALUE);
        StringBuilder mutation= new StringBuilder("mutation deleteTriple($id:ID){ deleteTriple(id:$id)")
                .append(graphQLTools.createQuery(new GraphQLTools.QueryTypeReference<Triple>(){}))
                .append("}");
        JsonMap<Object> params=JsonMap.createMap().append("operationName","deleteTriple")
                .append("query",mutation.toString())
                .append("variables",JsonMap.createMap().append("id",id));
        logger.info("url:"+userIamConfig.getIam());
        logger.info(params.jsonPettyString());
        RequestBody requestBody=RequestBody.create(params.jsonString(),mediaType);
        Request request=new Request.Builder().url(userIamConfig.getIam()).post(requestBody).build();
        try(Response response=httpClient.newCall(request).execute()){
            String body=response.body().string();
            return jsonMapper.readValue(body, new TypeReference<Map<String,Map<String,Triple>>>() {}).get("data").get("deleteTriple");
        }
    }

}
